//
//  AddtionalInfomationTipVC.swift
//  ezyform
//
//  Created by Moennig Q on 21/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import WebKit
class AddtionalInfomationTipVC: BaseViewController {
    let webView = WKWebView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.loadData()
    }
    func initUI() {
        self.title = R.string.localizable.additionalUpgradeSystemInformation()
        self.view.backgroundColor = .white
        self.view.addSubview(self.webView)
        self.webView.snp.makeConstraints { make in
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
            make.left.right.bottom.equalToSuperview()
        }
    }
    private func loadData(){
        let viewModel = EditJobInfoService()
        viewModel.setBlockAfterNetwork({ (result , _) -> Void in
            ModelViewUtil.shared.hideLoadingView()
            if let content = result as? String{
                let htmlBody = self.getHtmlBody(content)
                self.webView.loadHTMLString(htmlBody, baseURL: nil)
            }
            
        }){ (code, error) -> Void in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showErrorInfo(code, error)
        }
        ModelViewUtil.shared.showLoadingView()
        viewModel.getAdditionalInfomationTipHtmlInfo()
    }
    private func getHtmlBody(_ content: String)->String {
        var html = ""
        let realFontSize: CGFloat = 15 * Constant.currentSizeScale
        //添加html头信息
        html.append(contentsOf:"<html lang=\"en\">\n" +
                            "<head>\n" +
                            "    <meta charset=\"UTF-8\">\n" +
                            "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=no\">\n" +
                            "   <style>img{max-width:100%;width:auto;height:auto}</style>" +
                            "</head>\n" +
                            "<body>");
        html.append(contentsOf:"<div style=\"font-size: \(realFontSize)px; color:rgba(54, 68, 82, 1);\">\n");
        html.append(contentsOf:content);
        html.append(contentsOf:"</div>\n");
                    //添加html结尾
        html.append(contentsOf:"</body>\n" +
                            "</html>");
        return html
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.setNavigationBarAppearence(true)//当前导航栏需要显示底部灰线
        
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.setNavigationBarAppearence(false)//退出此界面其他界面不显示底部灰线
    }
}
